//
//  CoLoginController.swift
//  travel
//
//  Created by tong on 16/7/14.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import UIKit
import Eelay

open class CoInputController: UIViewController,UITextFieldDelegate,UITextViewDelegate,EeTopStyleSupport{
    
    open var initTopStyle: EeTopStyle{
        get{
            
            return .transparent_light
        }
    }
    
    public let backView = UIImageView()
    public var currentT = UITextField()
    public var currentTextView:UITextView? = nil

    
    public var textFields = [UITextField]()
    
    public var frameV:CGRect?
    
    public var bottom_distance = Sheight
    
    public let inputBack = UIControl()
    
    public let cornerV = UIControl()
    
    
    public var bottom_distance_c = NSLayoutConstraint()
    
    public var fastRigister = false
    
    override open func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.eelay = [
            [backView,[ee.T.L.B.R]],
            [inputBack,[ee.width.height.X]],
        ]
        
        backView.img_name = "img_login"
        
        self.bottom_distance_c = NSLayoutConstraint(item: inputBack, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
        view.addConstraint(bottom_distance_c)
        
        inputBack.addSubview(cornerV)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        
        inputBack.addTarget(self, action: #selector(outEdit), for: .touchDown)
        cornerV.addTarget(self, action: #selector(outEdit), for: .touchDown)

        // Do any additional setup after loading the view.
    }
    
    @objc open func outEdit() {
        self.currentT.resignFirstResponder()
        self.currentTextView?.resignFirstResponder()
        
        
    }
    
    @objc func keyboardChange(_ notify:Notification)
    {
        
        let userInfo = notify.userInfo
        var animationDuration:TimeInterval = 0.25
//        var animationCurve:UIViewAnimationCurve = UIViewAnimationCurve.easeIn
        
        var keyboardEndFrame:CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
        let animationDuration_v = userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
        animationDuration = animationDuration_v as TimeInterval
        
        keyboardEndFrame = (userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        
        let boardHeight = keyboardEndFrame.size.height
        let just  = bottom_distance - boardHeight
        weak var wself = self

        if notify.name == UIResponder.keyboardWillShowNotification
        {
            if just < 80
            {
                let  p_just = 80 - just
                
                let oldc = self.bottom_distance_c.constant
                UIView.animate(withDuration: animationDuration,animations: {
                    
                    wself?.bottom_distance_c.constant = oldc - p_just
//                    wself?.bottom_distance_c.constant = -p_just
                    wself?.view.setNeedsLayout()
                    wself?.view.layoutIfNeeded()
                    wself?.inputBack.setNeedsLayout()
                    wself?.inputBack.layoutIfNeeded()
                    
                    }, completion: { (finish) in
                })
                return
            }
            
        }
        
        if notify.name == UIResponder.keyboardWillHideNotification
        {
        
            UIView.animate(withDuration: animationDuration, animations: { () -> Void in
                wself?.bottom_distance_c.constant = 0
                wself?.view.setNeedsLayout()
                wself?.view.layoutIfNeeded()
                wself?.inputBack.setNeedsLayout()
                wself?.inputBack.layoutIfNeeded()
            })
        }
    }
    


    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    override open func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        currentT.resignFirstResponder()
    }
    
    open func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.currentT = textField

        return true
    }
    
    open func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        currentT.resignFirstResponder()
        currentTextView?.resignFirstResponder()
        return true
    }
    
    
    open func textFieldDidBeginEditing(_ textField: UITextField) {
        self.currentT = textField
        
        let sv = textField.superview!
        let p = sv.convert(textField.frame, to: self.view)
        bottom_distance = view.frame.size.height - p.origin.y - p.size.height
    }
    
    
    open func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//        if string == " "{
//            return false
//        }
        return true
    }
    
    
    
    open func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        return true
    }
    
    open func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        currentTextView = textView
        currentT.resignFirstResponder()
        let sv = textView.superview!
        let p = sv.convert(textView.frame, to: self.view)
        print("----IIIII--|\(p)")
        bottom_distance = view.frame.size.height - p.origin.y - p.size.height

        return true
    }
    
    
//    public func textViewDidBeginEditing(_ textView: UITextView) {
//        self.currentTextView = textView
//        
//        let sv = textView.superview!
//        let p = sv.convert(textView.frame, to: self.view)
//        bottom_distance = view.frame.size.height - p.origin.y - p.size.height
//    }
}

open class TypeInputController: TypeInitController,UITextFieldDelegate,UITextViewDelegate{
    
    open override var initTopStyle: EeTopStyle{
        get{
            
            return .transparent_light
        }
    }
    
    public let backView = UIImageView()
    public var currentT = UITextField()
    public var currentTextView:UITextView? = nil

    
    public var textFields = [UITextField]()
    
    public var frameV:CGRect?
    
    public var bottom_distance = Sheight
    
    public let inputBack = UIControl()
    
    public let cornerV = UIControl()
    
    
    public var bottom_distance_c = NSLayoutConstraint()
    
    public var fastRigister = false
    
    override open func viewDidLoad() {
        super.viewDidLoad()
        
        
        jo_contentView.eelay = [
            [backView,[ee.T.L.B.R]],
            [inputBack,[ee.width.height.X]],
        ]
        
        backView.img_name = "img_login"
        
        self.bottom_distance_c = NSLayoutConstraint(item: inputBack, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
        view.addConstraint(bottom_distance_c)
        
        inputBack.addSubview(cornerV)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        
        inputBack.addTarget(self, action: #selector(outEdit), for: .touchDown)
        cornerV.addTarget(self, action: #selector(outEdit), for: .touchDown)

        // Do any additional setup after loading the view.
    }
    
    @objc open func outEdit() {
        self.currentT.resignFirstResponder()
        self.currentTextView?.resignFirstResponder()
        
        
    }
    
    @objc func keyboardChange(_ notify:Notification)
    {
        
        let userInfo = notify.userInfo
        var animationDuration:TimeInterval = 0.25
//        var animationCurve:UIViewAnimationCurve = UIViewAnimationCurve.easeIn
        
        var keyboardEndFrame:CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
        let animationDuration_v = userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
        animationDuration = animationDuration_v as TimeInterval
        
        keyboardEndFrame = (userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        
        let boardHeight = keyboardEndFrame.size.height
        let just  = bottom_distance - boardHeight
        weak var wself = self

        if notify.name == UIResponder.keyboardWillShowNotification
        {
            if just < 80
            {
                let  p_just = 80 - just
                
                let oldc = self.bottom_distance_c.constant
                UIView.animate(withDuration: animationDuration,animations: {
                    
                    wself?.bottom_distance_c.constant = oldc - p_just
//                    wself?.bottom_distance_c.constant = -p_just
                    wself?.jo_contentView.setNeedsLayout()
                    wself?.jo_contentView.layoutIfNeeded()
                    wself?.inputBack.setNeedsLayout()
                    wself?.inputBack.layoutIfNeeded()
                    
                    }, completion: { (finish) in
                })
                return
            }
            
        }
        
        if notify.name == UIResponder.keyboardWillHideNotification
        {
        
            UIView.animate(withDuration: animationDuration, animations: { () -> Void in
                wself?.bottom_distance_c.constant = 0
                wself?.jo_contentView.setNeedsLayout()
                wself?.jo_contentView.layoutIfNeeded()
                wself?.inputBack.setNeedsLayout()
                wself?.inputBack.layoutIfNeeded()
            })
        }
    }
    


    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    override open func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        currentT.resignFirstResponder()
    }
    
    open func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.currentT = textField

        return true
    }
    
    open func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        currentT.resignFirstResponder()
        currentTextView?.resignFirstResponder()
        return true
    }
    
    
    open func textFieldDidBeginEditing(_ textField: UITextField) {
        self.currentT = textField
        
        let sv = textField.superview!
        let p = sv.convert(textField.frame, to: self.view)
        bottom_distance = view.frame.size.height - p.origin.y - p.size.height
    }
    
    
    open func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//        if string == " "{
//            return false
//        }
        return true
    }
    
    
    
    open func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        return true
    }
    
    open func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        currentTextView = textView
        currentT.resignFirstResponder()
        let sv = textView.superview!
        let p = sv.convert(textView.frame, to: self.view)
        bottom_distance = view.frame.size.height - p.origin.y - p.size.height

        return true
    }
    
    
//    public func textViewDidBeginEditing(_ textView: UITextView) {
//        self.currentTextView = textView
//
//        let sv = textView.superview!
//        let p = sv.convert(textView.frame, to: self.view)
//        bottom_distance = view.frame.size.height - p.origin.y - p.size.height
//    }
}

